<?php
	require_once('functions.php');
	
	// ADDRESS FUNCTIONS
	function GetAddresses(){
		$conn = GetConnection();
		return $results = $conn -> query('SELECT * FROM WSP2012_Address');
	}
	
	function GetAddress($id){
		$conn= GetConnection();
		$sql = "SELECT * FROM WSP2012_Address WHERE address_id=$id ";
		$results = $conn -> query($sql);
		$row = $results -> fetch_assoc();
		$conn -> close();
		return $row;
	}
	
	function SaveAddress($row){
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$sql = "UPDATE `WSP2012_Address` " 
			.	"Set users_id='$row2[users_id]', updated_at='$row2[updated_at]', streetnumber='$row2[streetnumber]', city='$row2[city]', "
			.	"state='$row2[state]', zipcode='$row2[zipcode]' " 	
			.	"WHERE address_id='$row2[id]' ";
		$results = $conn -> query($sql);
		$error = $conn -> error;
		$conn -> close();
		
		return $error == '' ? true : array('SQL Error' => $error);	
	}
	
	function CreateAddress($row){
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$sql = "INSERT INTO `WSP2012_Address` "
			.		"(address_id, users_id, created_at, updated_at, streetnumber, city, state, zipcode) "
			.		"VALUES ('$row2[address_id]', '$row2[users_id]', '$row2[created_at]', '$row2[updated_at]', '$row2[streetnumber]', '$row2[city]', '$row2[state]', '$row2[zipcode]') ";
		$results = $conn -> query($sql);
		$error = $conn -> error;
		$conn -> close();	
		
		return $error == '' ? true : array('SQL Error' => $error);
	}
	
	function DeleteAddress($row, $id){
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$sql = "DELETE from WSP2012_Address WHERE address_id= '$row2[id]' ";
		$results = $conn -> query($sql);
		$error = $conn -> error;
		$conn -> close();	
		
		return $error == '' ? true : array('SQL Error' => $error);
	}
?>
